<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 左边的按钮 -->
        <div class="btn">
          <el-button type="primary" round @click="addPermissionBtn(1,'0','add')">添加权限</el-button>
          <el-button type="primary" round @click="test">测试动态添加路由</el-button>

        </div>
      </el-card>
      <!-- 写一个表格 -->
      <el-card>
        <el-table
          :data="list"
          row-key="id"
          style="width: 100%"
        >
          <el-table-column
            prop="name"
            label="名称"
            width="180"
          />
          <el-table-column
            prop="code"
            label="标识"
            width="180"
          />
          <el-table-column
            prop="description"
            label="描述"
          />
          <el-table-column
            fixed="right"
            label="操作"
            width="200"
          >
            <template slot-scope="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="addPermissionBtn(2,scope.row.id,'add')">添加</el-button>
              <el-button type="text" @click="addPermissionBtn(scope.row.type,scope.row.id,'edit')">编辑</el-button>
              <el-button v-if="scope.row.children.length === 0" type="text" @click="deletePermissionBtn(scope.row.id,scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog
        :title="isEdit?'编辑权限':'添加权限'"
        :visible.sync="dialogVisible"
        width="50%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :show-close="false"
      >
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="权限名称">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="form.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="form.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch v-model="form.enVisible" active-value="1" inactive-value="2" active-text="不启用" inactive-text="启用">/>
            </el-switch></el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeBtn">取 消</el-button>
          <el-button type="primary" @click="onSubmit">{{ isEdit?'编辑权限':'添加权限' }}</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
// 导入接口
import { getAllPermissionRequest, addPermissionRequest, deletePermissionRequest, getPermissionByIdRequest, updatePermissionRequest } from '@/api/permission'
// 导入将平铺数据转为树形结构的方法
import { transformListToTree2 } from '@/utils'
export default {
  data() {
    return {
      list: [],
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        type: 0,
        pid: ''
      },
      dialogVisible: false,
      isEdit: false
    }
  },
  // 页面创建是获取权限数据并渲染到页面
  created() {
    this.getPermission()
  },
  methods: {
    test() {
      // 在代码里面通过点击，使用js动态添加理由 --- addRoutes
      // this.$router.addRoutes({
      //   path: '/demo',
      //   component: () => import('@/views/demo.vue')
      // })
      console.log(123)
      this.$router.addRoutes([
        {
          path: '/demo',
          component: () => import('@/views/demo.vue')
        },
        {
          path: '/test',
          component: () => import('@/views/demo.vue')
        }
      ])
    },
    // 获取权限数据
    async getPermission() {
      try {
        const res = await getAllPermissionRequest()
        console.log('getAllPermissionRequest: ', res)
        if (res.success) {
          this.$message.success(res.message)
          this.list = transformListToTree2(res.data, '0')
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('getAllPermissionRequest: ', err)
      }
    },
    // 增加权限按钮
    addPermissionBtn(type, pid, AddorEdit) {
      if (AddorEdit === 'edit') {
        this.isEdit = true
        this.getPermissionById(pid)
      } else {
        this.isEdit = false
      }
      this.dialogVisible = true
      this.form.type = type
      this.form.pid = pid
    },
    async addPermission() {
      try {
        const res = await addPermissionRequest(this.form)
        console.log('addPermissionRequest: ', res)
        if (res.success) {
          this.$message.success(res.message)
          // 刷新页面,关闭弹窗
          this.dialogVisible = false
          this.getPermission()
          // this.updatePermission
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('addPermissionRequest: ', err)
      }
    },
    deletePermissionBtn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deletePermission(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async deletePermission(id) {
      try {
        const res = await deletePermissionRequest(id)
        console.log('deletePermissionRequest: ', res)
        if (res.success) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getPermission()
        }
      } catch (err) {
        console.log('deletePermissionRequest: ', err)
      }
    },
    async getPermissionById(id) {
      console.log(id)
      try {
        const res = await getPermissionByIdRequest(id)
        console.log('getPermissionByIdRequest: ', res)
        if (res.success) {
          this.$message.success(res.message)
          this.form = res.data
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('getPermissionByIdRequest: ', err)
      }
    },
    async updatePermission() {
      try {
        const res = await updatePermissionRequest(this.form)
        console.log('updatePermissionRequest: ', res)
        if (res.success) {
          this.$message.success(res.message)
          this.getPermission()
          this.dialogVisible = false
        }
      } catch (err) {
        console.log('updatePermissionRequest: ', err)
      }
    },
    onSubmit() {
      if (this.isEdit) {
        this.updatePermission()
      } else {
        this.addPermission()
      }
    },
    closeBtn() {
      this.dialogVisible = false
      this.$refs.form.resetFields()
      this.form = this.$options.data().form
    }
  }
}
</script>
<style lang="scss">
.btn {
  text-align: right;
}
</style>
